#include "testIntrinsicImage.h"
#include "cvxImage.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>
#include <list>
#include <stdio.h>
#include "nvmath.h"
#include "least_square.h"
#include "cvxQuadTree.h"
#include "cvxSf.h"
#include "cvxStroke.h"
#include "cvxIntrinsicSolver.h"
#include "cvxIo.h"
#include "cvxEMTreeSolver.h"

using namespace std;
using namespace cv;

#if 0
int main(int argc, char **argv)
{
	char *fileName = NULL;
	int stddevThreshold = 5;
	int sizeThrehold  = 256;
	float mininumShadingScale = 1.0;

	if(argc != 3)
	{
		printf("usage: IntrinsicImage imageFileName scale\n");
		return 1;
	}

	fileName = argv[1];
	mininumShadingScale = (float)atof(argv[2]);

	Mat img =  imread(fileName, 1);	
	assert(!img.empty());

	int h = img.rows;
	int w = img.cols;

	Mat showShading;
	Mat grayImage;
	cv::cvtColor(img, grayImage, CV_BGR2GRAY);

	CvxEMtreeSolver solver;
	solver.setImage(img);
	solver.computeEdgeMap(0.99);

	imshow("original image", img);
	imshow("gray image", grayImage);
	imshow("edge map", solver.m_edgeMap);


	double tt = clock();

	solver.SetETree(stddevThreshold, sizeThrehold, mininumShadingScale);
	solver.SaveColorQuadTree("colorQuadTree.png", true);

	solver.SolveETree(true);	
	
	printf("complete E tree solver\n");
	showShading = solver.m_shading.clone();				
	showShading.convertTo(showShading, CV_8U, 255.0);
//	imshow("Etree shading", showShading);

		
	solver.CalcMTree(true);
	solver.SolveMtree(true);

	showShading = solver.m_shading.clone();				
	showShading.convertTo(showShading, CV_8U, 255.0);
//	imshow("Mtree shading", showShading);

	printf("complete M tree solver\n");

	double tUpSample = clock();
	solver.UpsampleAvector();
	printf("upSample cost time %f ms\n", clock() - tUpSample);

	printf("cost time %f ms\n",clock() - tt);

	printf("after up sample a vector\n");
	showShading = solver.m_shading.clone();				
	showShading.convertTo(showShading, CV_8U, 255.0);
	imshow("upsample shading", showShading);
	imshow("upsample albedo", solver.m_albedo);

	imwrite(string(fileName).substr(0, strlen(fileName)- 4) + "_shading.png", showShading);
	imwrite(string(fileName).substr(0, strlen(fileName)- 4) + string("_albedo.png"), solver.m_albedo);

	cv::waitKey(0);

	return 1;
}

#endif